//
//  BKMainPhonePayViewCell.m
//  bestkeep
//
//  Created by 李明 on 2016/12/13.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKPhonePayViewCell.h"
#import "BKMainCommon.h"
@interface BKPhonePayViewCell ()
@property (nonatomic, strong) UIImageView *lineImageView;  //线
@property (nonatomic, strong) UILabel *titleLabel;  //付款详情 头Label
@property (nonatomic, strong) UILabel *facePriceInfoLabel; //订单详情 右label
@property (nonatomic, strong) UILabel *payByAliOrWX;//展示付款方式 右Label
@property (nonatomic, strong) UIImageView *payWayImage;//指示图片
@property (nonatomic, strong) UILabel *salePriceInfoLabel; //实际售价 右label；
@property (nonatomic, strong) UILabel *payWayOne; //选择支付方式label中的第一个
@property (nonatomic, strong) UIImageView *payWayOneImageView; //图片  右
@property (nonatomic, strong) UIImageView *payChooseImageView;
@property (nonatomic, strong) UILabel *firstPaySalePriceLabel;  //xxx实际支付金额
@end

@implementation BKPhonePayViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.lineImageView = [[UIImageView alloc] init];
        [self addSubview:self.lineImageView];
        self.lineImageView.backgroundColor = RGB(218, 218, 218);
    }
    return self;
}

- (void)getPhonePayOrderAlipayInfo:(OrderAlipayInfo *)orderAlipayInfo indexPath:(NSIndexPath *)indexPath pay_way_info:(NSString *)pay_way_info isOpen:(BOOL)isOpen {
    [self.lineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (indexPath.row == 0) {
            self.titleLabel.text = @"付款详情";
            make.left.mas_equalTo(self.mas_left);
            make.right.mas_equalTo(self.mas_right);
            make.height.mas_equalTo(0.5);
            make.bottom.mas_equalTo(self.mas_bottom);
        } else {
            make.left.mas_equalTo(self.mas_left).offset(16);
            make.right.mas_equalTo(self.mas_right).offset(-16);
            make.height.mas_equalTo(0.5);
            make.bottom.mas_equalTo(self.mas_bottom);
        }
    }];
     if (indexPath.row == 1) {
        self.facePriceInfoLabel.text = orderAlipayInfo.order_info;
    } else if (indexPath.row == 2) {
        self.payByAliOrWX.text = pay_way_info;
        
        CGAffineTransform transform;
        transform = CGAffineTransformMakeRotation(M_PI);
        
        [UIView animateWithDuration:0.3 animations:^{
            self.payWayImage.transform = transform;
        } completion:^(BOOL finished) {
            if (isOpen) {
                self.payWayImage.image = [UIImage imageNamed:@"BKPhonePayIsOpen_"];
            } else {
                self.payWayImage.image = [UIImage imageNamed:@"BKPhonePayIsClose_"];
            }
        }];
        self.payWayImage.transform = CGAffineTransformIdentity;

    } else if (indexPath.row == 3) {
        NSDictionary *dict = orderAlipayInfo.pay_way_info[0];
        NSString *pay_way = dict[@"pay_way"];
        self.lineImageView.hidden = NO;
        if (!isOpen) {
            pay_way = @"";
            self.lineImageView.hidden = YES;
        }
        self.payWayOne.text = pay_way;
        [self.payWayOne mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.payWayOneImageView.mas_right).offset(7.5);
            make.centerY.mas_equalTo(self.mas_centerY);
            if (isOpen) {
                make.height.mas_equalTo(14);
            } else {
                make.height.mas_equalTo(0);
            }
            make.width.mas_equalTo(80);
        }];
        [self.payWayOneImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(16);
            make.centerY.mas_equalTo(self.mas_centerY);
            if (isOpen) {
                make.width.mas_equalTo(18);
                make.height.mas_equalTo(18);
            }else {
                make.width.mas_equalTo(0);
                make.height.mas_equalTo(0);
            }
        }];
        [self.payChooseImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_right).offset(-16);
            make.centerY.mas_equalTo(self.mas_centerY);
            if (isOpen) {
                make.width.mas_equalTo(18);
                make.height.mas_equalTo(18);
            }else {
                make.width.mas_equalTo(0);
                make.height.mas_equalTo(0);
            }
        }];
        if ([pay_way_info isEqualToString:pay_way]) {
            self.payChooseImageView.image = [UIImage imageNamed:@"BKPhone_payPhoneSelect"];
        }else {
            self.payChooseImageView.image = [UIImage imageNamed:@"BKPhone_payPhoneUnselect"];
        }
        if ([pay_way hasSuffix:@"微信"]) {
            self.payWayOneImageView.image = [UIImage imageNamed:@"BKPhone_WXlogoPayPhone"];
        }else {
            self.payWayOneImageView.image = [UIImage imageNamed:@"BKPhone_ZFBlogoPayPhone"];
        }
    }else if (indexPath.row == 4) {
        
        NSDictionary *dic = orderAlipayInfo.pay_way_info[1];
        NSString *pay_way = dic[@"pay_way"];
        self.lineImageView.hidden = NO;
        if (!isOpen) {
            self.lineImageView.hidden = YES;
            pay_way = @"";
        }
        self.payWayOne.text = pay_way;
        [self.payWayOne mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.payWayOneImageView.mas_right).offset(7.5f);
            make.centerY.mas_equalTo(self.mas_centerY);
            if (isOpen) {
                make.height.mas_equalTo(14);
                
            }else {
                make.height.mas_equalTo(0);
            }
            make.width.mas_equalTo(80);
        }];
        [self.payWayOneImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(16);
            make.centerY.mas_equalTo(self.mas_centerY);
            if (isOpen) {
                make.height.mas_equalTo(18);
                make.width.mas_equalTo(18);
            }else {
                make.height.mas_equalTo(0);
                make.width.mas_equalTo(0);
            }
        }];
        [self.payChooseImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_right).offset(-16);
            make.centerY.mas_equalTo(self.mas_centerY);
            if (isOpen) {
                make.height.mas_equalTo(18);
                make.width.mas_equalTo(18);
            }else {
                make.height.mas_equalTo(0);
                make.width.mas_equalTo(0);
            }
        }];
        if ([pay_way_info isEqualToString:pay_way]) {
            self.payChooseImageView.image = [UIImage imageNamed:@"BKPhone_payPhoneSelect"];
        }else {
            self.payChooseImageView.image = [UIImage imageNamed:@"BKPhone_payPhoneUnselect"];
        }
        if ([pay_way hasSuffix:@"微信"]) {
            self.payWayOneImageView.image = [UIImage imageNamed:@"BKPhone_WXlogoPayPhone"];
        }else {
            self.payWayOneImageView.image = [UIImage imageNamed:@"BKPhone_ZFBlogoPayPhone"];
        }
        
    }else if (indexPath.row == 5) {
        
        if (orderAlipayInfo.privilege_info && orderAlipayInfo.privilege_info.length >0) {
            
            self.salePriceInfoLabel.text = [NSString stringWithFormat:@"%@",orderAlipayInfo.privilege_info];
            self.salePriceInfoLabel.textColor = HexColor(0xFF1010);
            [self setFirstLineTextIndentWithContent:self.salePriceInfoLabel];
            
            self.firstPaySalePriceLabel.text = [NSString stringWithFormat:@"%@元",orderAlipayInfo.order_amount];
            
        }else {
            self.salePriceInfoLabel.text = [NSString stringWithFormat:@"%@元",orderAlipayInfo.pay_amount];
        }
        
        self.lineImageView.hidden = YES;
    }
}

- (void)setFirstLineTextIndentWithContent:(UILabel*)contentLabel {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentLabel.text];
    
    [attributedString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} range:NSMakeRange(0, 5)];
    [attributedString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} range:NSMakeRange(5, contentLabel.text.length-5)];
    contentLabel.attributedText = attributedString;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = RGB(64, 64, 64);
        _titleLabel.font = UIFontLarge;
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.mas_centerX);
            make.centerY.mas_equalTo(self.mas_centerY);
            make.width.mas_equalTo(self.mas_width).multipliedBy(0.5);
            make.height.mas_equalTo(18);
        }];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_top);
            make.bottom.mas_equalTo(self.mas_bottom);
            make.right.mas_equalTo(self.mas_right);
            make.width.mas_equalTo(ScreenWidth / 3.0 * 2.0);
        }];
        UIImageView *leftImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BKPhone_button_"]];
        [self addSubview:leftImg];
        [leftImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.mas_centerY);
            make.left.mas_equalTo(self.mas_left).offset(16);
            make.width.mas_equalTo(15);
            make.height.mas_equalTo(15);
        }];
    }
    return _titleLabel;
}

- (UILabel *)facePriceInfoLabel {
    if (!_facePriceInfoLabel) {
        UILabel *orderInfoLabel = [[UILabel alloc] init];
        orderInfoLabel.text = @"订单详情";
        orderInfoLabel.textColor = RGB(152, 152, 152);
        orderInfoLabel.font = UIFontLarge;
        [self addSubview:orderInfoLabel];
        [orderInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(16);
            make.centerY.mas_equalTo(self.mas_centerY);
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(18);
        }];
        _facePriceInfoLabel = [[UILabel alloc] init];
        [self addSubview:_facePriceInfoLabel];
        _facePriceInfoLabel.font = UIFontLarge;
        _facePriceInfoLabel.textAlignment = NSTextAlignmentRight;
        _facePriceInfoLabel.textColor = RGB(64, 64, 64);
        [self.facePriceInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_right).offset(-16);
            make.centerY.mas_equalTo(orderInfoLabel.mas_centerY);
            make.left.mas_equalTo(orderInfoLabel.mas_right).offset(10);
            make.height.mas_equalTo(17);
        }];
    }
    return _facePriceInfoLabel;
}

- (UILabel *)payByAliOrWX {
    if (!_payByAliOrWX) {
        UILabel *payWayLabel = [[UILabel alloc] init];
        payWayLabel.text = @"付款方式";
        payWayLabel.textColor = RGB(152, 152, 152);
        payWayLabel.font = UIFontLarge;
        [self addSubview:payWayLabel];
        [payWayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(16);
            make.centerY.mas_equalTo(self.mas_centerY);
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(18);
        }];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_top);
            make.bottom.mas_equalTo(self.mas_bottom);
            make.left.mas_equalTo(self.mas_left);
            make.width.mas_equalTo(ScreenWidth / 3.0 * 2.0);
        }];
        _payByAliOrWX = [[UILabel alloc] init];
        [self addSubview:_payByAliOrWX];
        _payByAliOrWX.font = UIFontLarge;
        _payByAliOrWX.textAlignment = NSTextAlignmentRight;
        _payByAliOrWX.textColor = RGB(64, 64, 64);
        [_payByAliOrWX mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_right).offset(-33);
            make.centerY.mas_equalTo(payWayLabel.mas_centerY);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(20);
        }];
    }
    return _payByAliOrWX;
}

- (UIImageView *)payWayImage {
    if (!_payWayImage) {
        _payWayImage = [[UIImageView alloc] init];//order_arrow_up_ //order_down_
        [_payWayImage setContentMode:UIViewContentModeScaleAspectFit];
        _payWayImage.image = [UIImage imageNamed:@"BKPhonePayIsOpen_"];
        [self addSubview:_payWayImage];
        [_payWayImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.mas_centerY);
            make.right.mas_equalTo(self.mas_right).offset(-16);
            make.width.mas_equalTo(10);
            make.height.mas_equalTo(24.0f/41.0f*10);
            
        }];
    }
    return _payWayImage;
}

- (UILabel *)salePriceInfoLabel {
    if (!_salePriceInfoLabel) {
        UILabel *payInfoLabel  = [[UILabel alloc] init];
        payInfoLabel.text = @"需付款";
        payInfoLabel.font = UIFontLarge;
        [self addSubview:payInfoLabel];
        [payInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(16);
            make.top.mas_equalTo(self.mas_top).offset(32);
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(17);
        }];
        
        _salePriceInfoLabel = [[UILabel alloc] init];
        _salePriceInfoLabel.font = UIFontLarge;
        [_salePriceInfoLabel setTextAlignment:NSTextAlignmentRight];
        [self addSubview:_salePriceInfoLabel];
        [_salePriceInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_right).offset(-16);
            make.centerY.mas_equalTo(payInfoLabel.mas_centerY);
            make.width.mas_equalTo(150);
            make.height.mas_equalTo(17);
        }];
        self.salePriceInfoLabel.textColor = RGB(64, 64, 64);
    }
    return _salePriceInfoLabel;
}

- (UILabel *)payWayOne {
    if (!_payWayOne) {
        _payWayOne = [[UILabel alloc] init];
        _payWayOne.font = [UIFont systemFontOfSize:14];
        _payWayOne.textColor = RGB(64, 64, 64);
        [_payWayOne setTextAlignment:NSTextAlignmentLeft];
        [self addSubview:_payWayOne];
    }
    return _payWayOne;
}

- (UIImageView *)payWayOneImageView {
    if (!_payWayOneImageView) {
        _payWayOneImageView = [[UIImageView alloc] init];
        [_payWayOneImageView setContentMode:UIViewContentModeScaleAspectFit];
        [self addSubview:_payWayOneImageView];
    }
    return _payWayOneImageView;
}

- (UIImageView *)payChooseImageView {
    if (!_payChooseImageView) {
        _payChooseImageView = [[UIImageView alloc] init];
        [_payChooseImageView setContentMode:UIViewContentModeScaleAspectFit];
        [self addSubview:_payChooseImageView];
    }
    return _payChooseImageView;
}

- (UILabel *)firstPaySalePriceLabel {
    if (!_firstPaySalePriceLabel) {
        _firstPaySalePriceLabel = [[UILabel alloc] init];
        [self addSubview:_firstPaySalePriceLabel];
        _firstPaySalePriceLabel.font = UIFontSmall;
        _firstPaySalePriceLabel.textColor = RGB(173, 173, 173);
        [_firstPaySalePriceLabel setTextAlignment:NSTextAlignmentRight];
        [_firstPaySalePriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_right).offset(-16);
            make.height.mas_equalTo(12);
            make.bottom.mas_equalTo(self.salePriceInfoLabel.mas_top).offset(-10);
        }];
        
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.backgroundColor = RGB(173, 173, 173);
        [self addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(_firstPaySalePriceLabel.mas_width);
            make.centerX.mas_equalTo(_firstPaySalePriceLabel.mas_centerX);
            make.centerY.mas_equalTo(_firstPaySalePriceLabel.mas_centerY);
            make.height.mas_equalTo(1.0f);
        }];
    }
    return _firstPaySalePriceLabel;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
